package com.heer.gymnasium.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.Interface.OnHeightChangeListener;
import com.heer.gymnasium.R;
import com.heer.gymnasium.activity.SiteReservedActivity;
import com.heer.gymnasium.activity.SiteReservedInfoActivity;
import com.heer.gymnasium.adapter.SiteDetailsType1Adapter;
import com.heer.gymnasium.adapter.SiteDetailsTypeAdapter;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.util.ScreenUtils;
import com.heer.gymnasium.util.ViewUtils;
import com.heer.model.SitePlaceBookedBean;
import com.heer.model.SitePlaceInfoBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

import static com.heer.gymnasium.constant.Extras.EXTRA_ID;
import static com.heer.gymnasium.constant.Extras.EXTRA_TYPE;

/**
 * Created by heer on 17/4/11.
 */

public class SiteDetailsTypeFragment extends BaseFragment {
    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.image_arrow)
    ImageView imageArrow;

    private SiteDetailsTypeAdapter mAdapter;
    private SiteDetailsType1Adapter mAdapter1;
    private OnHeightChangeListener listener;

    private String id;
    private String type;

    public static SiteDetailsTypeFragment newInstance(String id, String isding) {
        SiteDetailsTypeFragment fragment = new SiteDetailsTypeFragment();
        Bundle args = new Bundle();
        args.putString(EXTRA_ID, id);
        args.putString(EXTRA_TYPE, isding);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments() != null ? getArguments().getString(EXTRA_ID) : "";
        type = getArguments() != null ? getArguments().getString(EXTRA_TYPE) : "";
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            listener = (OnHeightChangeListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + "must implement OnHeightChangeListener");
        }
    }

    @Override
    protected int setConvertView() {
        return R.layout.fragment_site_details_booked;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initView() {
        super.initView();
        if (TextUtils.equals(type, Type.SITE_TYPE_TICKET)) {
            list.setAdapter(mAdapter = new SiteDetailsTypeAdapter(mContext));
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long ids) {
                    SitePlaceInfoBean.Ticket item = mAdapter.getItem(position);
                    SiteReservedInfoActivity.toReservedInfo(mContext, item.getId(), item.getPrice());
                }
            });
        } else if (TextUtils.equals(type, Type.SITE_TYPE_BOOKED)) {
            list.setAdapter(mAdapter1 = new SiteDetailsType1Adapter(mContext));
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long ids) {
                    SiteReservedActivity.siteReserved(mContext, id, mAdapter1.getItem(position), mAdapter1.getItemList());
                    //                        SiteReservedActivity.siteReserved(mContext, mAdapter.getItem(position).getId());
                }
            });
        }
    }

    @Override
    protected void loadData(boolean loadmore) {
        if (TextUtils.equals(type, Type.SITE_TYPE_TICKET)) {
            mAppHelper.apiService().getSitePlaceInfoList(id).enqueue(new BaseCallback<ApiResponse<List<SitePlaceInfoBean>>>() {
                @Override
                public void onResponse(Call<ApiResponse<List<SitePlaceInfoBean>>> call, Response<ApiResponse<List<SitePlaceInfoBean>>> response) {
                    super.onResponse(call, response);
                    if (response.isSuccessful() && response.body().isSccessful()) {
                        List<SitePlaceInfoBean> placeList = response.body().getData();
                        if (null != placeList && placeList.size() > 0) {
                            List<SitePlaceInfoBean.Ticket> tickets = new ArrayList<>();
                            for (SitePlaceInfoBean item : placeList) {
                                for (SitePlaceInfoBean.Ticket ticket : item.getTicket()) {
                                    tickets.add(ticket);
                                }
                            }
                            mAdapter.setItems(tickets);
                            mAdapter.setCount(tickets.size() > 2 ? 2 : tickets.size());

                            if (tickets.size() <= 2) {
                                imageArrow.setVisibility(View.GONE);
                            }

                            listener.onHeightChange(id, ViewUtils.getListViewHeightBasedOnChildren(list) + ScreenUtils.dp2px(24));
                        }
                    }
                    if (response.body().isTokenExpire()) {
                        toLogin();
                    }
                }

                @Override
                public void onFailure(Call<ApiResponse<List<SitePlaceInfoBean>>> call, Throwable t) {
                    super.onFailure(call, t);
                }
            });
        } else if (TextUtils.equals(type, Type.SITE_TYPE_BOOKED)) {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = sDateFormat.format(new Date());
            mAppHelper.apiService().getSitePlaceBookedList(id, date, 30).enqueue(new BaseCallback<ApiResponse<List<SitePlaceBookedBean>>>() {
                @Override
                public void onResponse(Call<ApiResponse<List<SitePlaceBookedBean>>> call, Response<ApiResponse<List<SitePlaceBookedBean>>> response) {
                    super.onResponse(call, response);
                    if (response.isSuccessful() && response.body().isSccessful()) {
                        List<SitePlaceBookedBean> placeList = response.body().getData();
                        if (null != placeList && placeList.size() > 0) {
                            mAdapter1.setItems(placeList);
                            mAdapter1.setCount(placeList.size() > 5 ? 5 : placeList.size());

                            if (placeList.size() <= 5) {
                                imageArrow.setVisibility(View.GONE);
                            }

                            listener.onHeightChange(id, ViewUtils.getListViewHeightBasedOnChildren(list) + ScreenUtils.dp2px(24));
                        }
                    }
                    if (response.body().isTokenExpire()) {
                        toLogin();
                    }
                }

                @Override
                public void onFailure(Call<ApiResponse<List<SitePlaceBookedBean>>> call, Throwable t) {
                    super.onFailure(call, t);
                }
            });
        }

    }

    @OnClick(R.id.image_arrow)
    public void onClick() {
        if (TextUtils.equals(type, Type.SITE_TYPE_TICKET)) {
            if (mAdapter.getCount() == 2) {
                mAdapter.setCount(mAdapter.getItemList().size());
                imageArrow.setImageResource(R.mipmap.arrow_top);
            } else {
                mAdapter.setCount(2);
                imageArrow.setImageResource(R.mipmap.arrow_down);
            }
        } else if (TextUtils.equals(type, Type.SITE_TYPE_BOOKED)) {
            if (mAdapter1.getCount() == 5) {
                mAdapter1.setCount(mAdapter1.getItemList().size());
                imageArrow.setImageResource(R.mipmap.arrow_top);
            } else {
                mAdapter1.setCount(5);
                imageArrow.setImageResource(R.mipmap.arrow_down);
            }
        }

        listener.onHeightChange(id, ViewUtils.getListViewHeightBasedOnChildren(list) + ScreenUtils.dp2px(24));
    }
}
